SharedFlow
and MutableSharedFlow
were added to coroutines with version 1.4.0.
A SharedFlow
is a hot flow (one that can exist outside of any subscribers)
that can be consumed by multiple subscribers.
The holder of a SharedFlow
can use Flow
-like constructs to observe
the flow (e.g., collect()
).
The typical way to set up a SharedFlow
is to use a MutableSharedFlow
.
You can call emit()
on the MutableSharedFlow
to send a value to all
current subscribers.
Here, we set up a MutableSharedFlow
that will emit 10 random numbers. And,
we set up two subscribers to that flow. Both subscribers will collect the same
set of random values, since they are both observing the same flow.
You can learn more about this in:
Tags: